package org.jamailia.core.service;

import org.jamailia.core.model.BaseEntity;
import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.jamailia.core.service.OrmEnabledService;

/**
 * EntityService.
 *
 * @author Rene Gielen
 */
public abstract class EntityService extends OrmEnabledService {

    private static final Logger LOG = Logger.getLogger(EntityService.class);

    public abstract Class featuredEntityClass();

    protected Criteria createCriteria() {
        return getGenericDao().createCriteria(featuredEntityClass());
    }

    protected Criteria createCriteriaByExample( Object filter ) {
        if (filter == null) {
            try {
                filter = featuredEntityClass().newInstance();
            } catch ( Exception e ) {
                LOG.error("EntityService - [createCriteriaByExample]: Exception catched: ", e);
            }
        }
        return getGenericDao().createCriteriaByExample(filter);
    }

    @SuppressWarnings("unchecked")
    public <T extends BaseEntity> T get( Long id ) {
        return (T) getGenericDao().get(featuredEntityClass(), id);
    }

}
